<?php
namespace app\backstage\controller;

use think\Controller;
use think\Db;
use think\facade\Request;

class Version extends Controller
{

    protected $middleware = [
        'VerifyJwt' => ['except' => []],
        'VerifyPower' => ['except' => ['getRandomExamineNovels']],
    ];

    public function index()
    {
        $apps = Db::table('quick_app')->field('id,name')->select();

        $this->assign('apps', $apps);
        $this->assign('channels', sonUsers('channel'));
        return $this->fetch();
    }

    public function list_data()
    {
        $params = Request::param('', '', 'strip_tags');
        $Version = \think\facade\App::model('Version', 'logic');
        $result = $Version->getList($params);

        return sucJson($result['list'], $result['count']);
    }

    public function add()
    {
        if( Request::isGet() ){

            $apps = Db::table('quick_app')->field('id,name')->select();
            $this->assign('apps', $apps);
            $this->assign('channels', sonUsers('channel'));
            return $this->fetch();
        }

        $params = Request::post('','','strip_tags,trim');
        $verify_result = $this->validate($params, 'Feature.add');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Version = \think\facade\App::model('Version','logic');

        $where[] = ['quick_id','=',$params['quick_id']];
        $where[] = ['channel_id','=',$params['channel_id']];

        $record = $Version->getInfo($where);
        if($record){
            return json(['code'=>101,'msg'=>'该版本信息已存在，请勿重复添加']);
        }

        $data   = $Version->dataProcessing($params);
        $result = $Version->addInfo($data['data']);

        return json($result);
    }

    /*信息预览*/
    public function preview()
    {
        $params = Request::param('', '', 'strip_tags');

        $where[] = ['id','=',$params['id']];

        $Version = \think\facade\App::model('Version','logic');
        $info = $Version->getInfo($where);

        if(!$info){
            $this->error('该版本信息记录不存在');
        }

        $apps = Db::table('quick_app')->field('id,name')->select();

        $this->assign('channels', sonUsers('channel'));
        $this->assign('apps', $apps);
        $this->assign('info',$info);
        return $this->fetch();
    }

    /*数据编辑*/
    public function edit()
    {
        $params = Request::post('','','strip_tags,trim');
        $verify_result = $this->validate($params, 'Feature.edit');

        if(true !== $verify_result){
            return json(['code'=>101,'msg'=>$verify_result]);
        }

        $Version = \think\facade\App::model('Version','logic');

        $where[] = ['version','=',$params['version']];
        $where[] = ['quick_id','=',$params['quick_id']];
        $where[] = ['channel_id','=',$params['channel_id']];

        $record = $Version->getInfo($where);
        if($record && $record['id'] != $params['id']){
            return json(['code'=>101,'msg'=>'该版本信息已存在，请勿重复添加']);
        }

        $where_u[] = ['id','=',$params['id']];

        $data = $Version->dataProcessing($params);

        $result = $Version->editInfo($where_u,$data['data']);

        return json($result);
    }

    /*数据删除*/
    public function delete()
    {
        $params = Request::param();
        $verify_result = $this->validate($params, 'Feature.info');

        if(true !== $verify_result){
            $this->error($verify_result);
        }

        $Version = \think\facade\App::model('Version','logic');

        $where[] = ['id','in',$params['id']];
        $result  = $Version->delInfo($where);

        return json($result);
    }

}